package com.personal.controller;


import com.personal.controller.result.BackCode;
import com.personal.controller.result.Result;
import com.personal.domain.Individual;
import com.personal.service.IndividualService;
import com.personal.util.partDisplay.IndividualPart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/individual")
public class IndividualController {

    @Autowired
    private IndividualService individualService;

    @GetMapping("/All")
    public Result selectAll(){
        List<Individual> list=individualService.selectAll();
        return new Result(list!=null? BackCode.GET_OK:BackCode.GET_ERR,list);
    }

    @GetMapping()
    public Result selectPartAll(){
        List<IndividualPart> list=individualService.selectPartAll();
        return new Result(list!=null?BackCode.GET_OK:BackCode.GET_ERR,list);
    }

    @GetMapping("/search")
    public Result selectPartByKey(@RequestParam("name")String name,@RequestParam("code")String code){
        List<IndividualPart> list=individualService.selectPartByKey(name,code);
        return new Result(list!=null?BackCode.GET_OK:BackCode.GET_ERR,list);
    }

    @PostMapping()
    public Result add(@RequestBody Individual individual){
        boolean flag= individualService.add(individual);
        return new Result(flag?BackCode.SAVE_OK:BackCode.SAVE_ERR,flag);
    }

    @GetMapping("updateData")
    public Result getUpdateData(@RequestParam("id") int id){
        Individual individual=individualService.getUpdateData(id);
        return new Result(individual!=null?BackCode.GET_OK:BackCode.GET_ERR,individual);
    }
    @PutMapping()
    public Result update(@RequestBody Individual individual){
        boolean flag=individualService.update(individual);
        return new Result(flag?BackCode.UPDATE_OK:BackCode.UPDATE_ERR,flag);
    }


    @DeleteMapping()
    public Result delete(@RequestParam List<Integer> ids){
        boolean flag=individualService.delete(ids);
        return new Result(flag?BackCode.DELETE_OK:BackCode.DELETE_ERR,flag);
    }

}
